package cn.itcast.service.impl;

import cn.itcast.Role;
import cn.itcast.UserInfo;
import cn.itcast.dao.RoleDao;
import cn.itcast.dao.UserDao;
import cn.itcast.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

     @Autowired
    private UserDao userDao;

     @Autowired
     private RoleDao roleDao;

     @Autowired
     private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 从数据库中获取的对象，封装成框架中的对象。
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserInfo userInfo = null;

        try {
            userInfo =  userDao.findByUsername(username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //处理框架中自己的对象封装成UserDetails
        User user = new User(userInfo.getUsername(),userInfo.getPassword(),getAuthority(userInfo.getRoles()));

        return user;
    }

    /**
     * 将数据库中的获取的对象中的角色属性取出用框架中的类来进行描述
     * @param roles
     * @return
     */

    public List<SimpleGrantedAuthority> getAuthority(List<Role> roles){
        List<SimpleGrantedAuthority> list = new ArrayList<>();
        for (Role role : roles) {
            list.add(new SimpleGrantedAuthority("ROLE_"+role.getRoleName()));
        }
        return list;
    }

    /**
     * 查询所有的方法
     * @return
     */
    @Override
    public List<UserInfo> findAll() throws Exception {
        return userDao.findAll();
    }


    /**
     * 添加用户
     * @param userInfo
     * @throws Exception
     */
    @Override
    public void save(UserInfo userInfo) throws Exception {
    //对密码加密处理；
        userInfo.setPassword(bCryptPasswordEncoder.encode(userInfo.getPassword()));
        userDao.save(userInfo);
    }


    /**
     * 根据id查询用户
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public UserInfo findById(String id) throws Exception {
        return userDao.findById(id);
    }

    /**
     * 根据用户id查询该用户可以添加的角色
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public List<Role> findOtherRoles(String userId) throws Exception {
        return roleDao.findOtherRoles(userId);
    }

    /**
     * 给用户添加角色
     * @param userId
     * @param roleIds
     */
    @Override
    public void addRoleToUser(String userId, String[] roleIds) throws Exception {

        for (String roleId : roleIds) {
            userDao.addRoleToUser(userId,roleId);
        }

    }

    /**
     * 批量删除用户
     * @param userIds
     * @throws Exception
     */
    @Override
    public void delByIds(String[] userIds) throws Exception {
        for (String userId : userIds) {
            //先删除users_role表中的外键
            userDao.delURByUserId(userId);
            userDao.delUserByUserId(userId);
        }

    }
}
